package com.lzx.spring3.aop.cglib;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.lzx.spring3.meta.Watch;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CGlibProxyFactory {

	public static <T> T createProxyInstance(Object targetObject,
			Class<T> requiredType) {
		// 传入用户类

		Enhancer enhancer = new Enhancer(); // Enhancer是cglib的核心类

		// 将用户类设为 Enhancer对象的superclass属性,,即设为 Enhancer对象的父类
		enhancer.setSuperclass(targetObject.getClass());
		// 设 Enhancer对象的Callbacks属性,要求必须是Callback接口类型
		enhancer.setCallback(new MethodInterceptorImpl(targetObject));

		return (T) enhancer.create(); // 生成代理对象
	}

	private static class MethodInterceptorImpl implements MethodInterceptor {
		private Object targetObject;

		public MethodInterceptorImpl(Object targetObject) {
			this.targetObject = targetObject;
		}

		public Object intercept(Object obj, Method method, Object[] args,
				MethodProxy proxy) throws Throwable {
//			method.setAccessible(true);
	
//			Object result = proxy.invoke(targetObject, args);
			
			return null;
		}

		public void showMethod(Method method) {
			StringBuilder sb = new StringBuilder();
			Class<?>[] types = method.getParameterTypes();
			for (int i = 0; i < types.length; i++) {
				Class<?> clz = types[i];
				if (i > 0) {
					sb.append(", ");
				}
				sb.append(clz.getSimpleName());

			}
			System.out.println(method.getName() + "(" + sb.toString() + ")");
		}

	}

}